package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysMultiQuestionDao;
import com.groupfifteen.entity.SysMultiQuestionEntity;
import com.groupfifteen.service.SysMultiQuestionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysMultiQuestionService")
public class SysMultiQuestionServiceImpl extends ServiceImpl<SysMultiQuestionDao, SysMultiQuestionEntity>
        implements SysMultiQuestionService {

    private final SysMultiQuestionDao sysMultiQuestionDao;

    public SysMultiQuestionServiceImpl(SysMultiQuestionDao sysMultiQuestionDao) {
        this.sysMultiQuestionDao = sysMultiQuestionDao;
    }

    @Override
    public IPage<SysMultiQuestionEntity> QuerySysMultiQuestions(SysMultiQuestionEntity sysMultiQuestionEntity, int pageNo, int pageSize) {
        LambdaQueryWrapper<SysMultiQuestionEntity> query = Wrappers.lambdaQuery();
        // 假设我们根据questionId进行查询，您可以根据实际需求添加更多的查询条件
        query.eq(sysMultiQuestionEntity.getQuestionId() != null, SysMultiQuestionEntity::getQuestionId, sysMultiQuestionEntity.getQuestionId());

        // 确保页码和页大小有效
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 10;
        }

        // 创建分页对象
        IPage<SysMultiQuestionEntity> page = new Page<>(pageNo, pageSize);
        // 执行分页查询
        return baseMapper.selectPage(page, query);
    }

    // 在这里实现 SysMultiQuestionService 接口中的其他方法

    @Override
    public Integer AddMultiQuestion(SysMultiQuestionEntity sysMultiQuestionEntity) {
        return baseMapper.insert(sysMultiQuestionEntity);
    }

    @Override
    public Integer DeleteMultiQuestion(Integer question_id) {
        return baseMapper.deleteById(question_id);
    }

    @Override
    public SysMultiQuestionEntity GetMultiQuestion(Integer question_id) {
        return baseMapper.selectById(question_id);
    }

    @Override
    public Integer UpdateMultiQuestion(SysMultiQuestionEntity sysMultiQuestionEntity) {
        return baseMapper.updateById(sysMultiQuestionEntity);
    }

    /**
     * 通过试卷id 查询试卷
     *
     * @param paperId 试卷id
     * @return 问题
     */
    @Override
    public List<SysMultiQuestionEntity> findByIdAndType(Integer paperId) {
        return sysMultiQuestionDao.findByIdAndType(paperId);
    }
    /**
     * 通过学科查询
     *
     * @param subject 学科
     * @param pageNo 分页数
     * @return 题目id
     */
    @Override
    public List<Integer> findBySubject(String subject, Integer pageNo) {
        return sysMultiQuestionDao.findBySubject(subject,pageNo);
    }
    /**
     * 通过id查询选择题
     *
     * @return 选择题
     */
    @Override
    public SysMultiQuestionEntity findOnlyQuestionId() {

        return sysMultiQuestionDao.findOnlyQuestionId();
    }
}